<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//items
//	id,type,name,value
//items_values
//	items_id,id,type,value
//		type
//		0	default
//		1	custom

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class BaseItemValuesItemBase extends BaseItemBase {
	
	//constructor
	function BaseItemValuesItemBase() {
		parent::BaseItemBase();
		
		$this->table = 'pp_contents_items';
		$this->prefix = 'items_';
		
		$this->table_value = 'pp_contents_items_values';
		$this->prefix_value = 'items_values_';
		
		$this->_table_owner_field = 'owner';
		$this->_table_owner_prefix = '';
		$this->_table_owner_usable = true;
		$this->_table_status_usable = false;
		
		$this->search_fields = array('name', 'value');
		$this->filter_fields = array('=types_id:=', '=categories_id:=');
		
		//
		$this->ivObj = NULL;
	}
	
	//methods
	function getItemsValues($filter = '', $check = 0, $limit = true, $type = 0) {
		$items_values = array();
		$items = $this->getItems($filter, $check, $limit);
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$items_values[] = array('item' => $items[$i], 'values' => $this->getItemValues($items[$i]['items_id'], $check, $type));
		}
		
		return $items_values;
	}
	
	function getItemsAndValues($filter = '', $check = 0, $limit = true, $type = 0) {
		return $this->getItemsValues($filter, $check, $limit, $type);
	}
	
	//
	function getItemValue($items_values_id, $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			$query_str = " and items_values_status = 1";
		}
		$query = "select * from pp_contents_items_values where items_values_id = '" . (int)$items_values_id . "' " . $query_str;
		
		return $this->adb->getRow($query);
	}
	
	function getItemValues($items_id, $check = 0, $type = 0) {
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			$query_str = " and items_values_status = 1";
		}
		if( $type >= 0 ) {
			$query_str .= " and items_values_type = '" . (int)$type . "'";
		}
		$query = "select * from pp_contents_items_values where items_id = '" . (int)$items_id . "' " . $query_str;
		
		return $this->adb->getRows($query);
	}
	
	function getItemValuesIDs($items_id, $check = 0, $type = 0) {
		$ids = array();
		$items = $this->getItemValues($items_id, $check, $type);
		if( is_array($items) ) {
			for($i=0, $n=sizeof($items); $i<$n; $i++) {
				$ids[] = $items[$i]['items_values_id'];
			}
		}
		
		return $ids;
	}
	
	function getItemAndValues($items_id, $check = 0, $type = 0) {
		$item_values = array();
		$item = $this->getItem($items_id, 'id', $check);
		if( $item['items_id'] > 0 ) {
			$values = $this->getItemValues($item['items_id'], $check, $type);
			$item_values = array('item' => $item, 'values' => $values);
		}
		
		return $item_values;
	}
	
	//
	function addItemValue($items_id, $data_array, $type = 0) {
		$data_array['items_id'] = $items_id;
		$data_array['items_values_type'] = $type;
		
		return $this->ivObj->addItem($data_array);
	}
	
	function addItemValues($items_id, $data_array, $choice = 0, $type = 0) {
		$items_values_ids = array();
		$values = $data_array['items_values_value'];
		$values = $this->_choice_values($choice, $values);
		if( !is_array($values) ) {
			$values = array($values);
		}
		foreach($values as $value) {
			if( strlen($value) > 0 ) {
				$d = array();
				$d['items_values_value'] = $value;
				$items_values_ids[] = $this->addItemValue($items_id, $d, $type);
			}
		}
		
		return $items_values_ids;
	}
	
	function addItemsValues($data_array, $choice = 0, $type = 0) {
		$items_values_ids = array();
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				$ids = $this->addItemValues($k, $v, $choice, $type);
				if( is_array($ids) ) {
					if( is_array($items_values_ids[$k]) ) {
						$items_values_ids[$k] = array_merge($items_values_ids[$k], $ids);
					} else {
						$items_values_ids[$k] = $ids;
					}
				}
			}
		}
		
		return $items_values_ids;
	}
	
	function addItemAndValues($data_array, $choice = 0, $type = 0) {
		$items_id = $this->addItem($data_array);
		if( isset($data_array['items_choice']) ) {
			$choice = $data_array['items_choice'];
		}
		$d = array();
		$d['items_values_type'] = $type;
		$d['items_values_value'] = $data_array['items_value'];
		$items_values_ids = $this->addItemValues($items_id, $d, $choice, $type);
		
		return array($items_id, $items_values_ids);
	}
	
	//
	function updateItemValue($items_values_id, $data_array) {
		return $this->ivObj->updateItem($items_values_id, $data_array);
	}
	
	function updateItemValues($items_id, $data_array, $choice = 0, $type = 0) {
		$idsb = array();
		$ids = $this->getItemValuesIDs($items_id, -1, $type);
		for($i=0, $n=sizeof($data_array['values']); $i<$n; $i++) {
			if( is_array($data_array['values'][$i]) ) {
				$value = $data_array['values'][$i];
				if( $value['items_values_id'] == 0 ) {
					if( $type >= 0 ) {
						$value['items_values_type'] = $type;
					}
					
					$this->addItemValues($items_id, $value, $item_data_array['items_choice'], $type);
				} elseif( in_array($value['items_values_id'], $ids) ) {
					$this->updateItemValue($value['items_values_id'], $value);
					
					$idsb[] = $value['items_values_id'];
				}
			}
		}
		if( sizeof($idsb) != sizeof($ids) ) {
			$a = array_values(array_diff($ids, $idsb));
			$this->adb->delete("delete from " . $this->table_value . " where items_values_id in (" . $this->_implode(',', $a) . ")");
		}
		
		return true;
	}
	
	function updateItemAndValues($items_id, $data_array, $type = 0) {
		$data_array = func_db_input($data_array);
		
		$item_data_array = $data_array['item'];
		if( is_array($item_data_array['items_value']) ) {
			$item_data_array['items_value'] = implode("\n", $item_data_array['items_value']);
		}
		$item_data_array['items_last_modified'] = 'now()';
		$item_data_array['items_modified_by'] = $this->_get_current_user();
		
		$result = $this->adb->updateA($this->table, $item_data_array, "items_id = '" . (int)$items_id . "'");
		
		//
		$idsb = array();
		$ids = $this->getItemValuesIDs($items_id, -1, $type);
		for($i=0, $n=sizeof($data_array['values']); $i<$n; $i++) {
			if( is_array($data_array['values'][$i]) ) {
				$value = $data_array['values'][$i];
				if( $value['items_values_id'] == 0 ) {
					if( $type >= 0 ) {
						$value['items_values_type'] = $type;
					}
					
					$this->addItemValues($items_id, $value, $item_data_array['items_choice'], $type);
				} elseif( in_array($value['items_values_id'], $ids) ) {
					$this->updateItemValue($value['items_values_id'], $value);
					
					$idsb[] = $value['items_values_id'];
				}
			}
		}
		if( sizeof($idsb) != sizeof($ids) ) {
			$a = array_values(array_diff($ids, $idsb));
			$this->adb->delete("delete from " . $this->table_value . " where items_values_id in (" . $this->_implode(',', $a) . ")");
		}
		
		return true;
	}
	
	//events
	function onDeleteItemsAfter($items_ids) {
		$query = "delete from " . $this->table_value . " where items_id in (" . $this->_implode(',', $items_ids) . ")";
		
		return $this->adb->delete($query);
	}
	
	//category
	function getCategoryID($items_id) {
		$query = "select categories_id from pp_contents_items where items_id = '" . (int)$items_id . "' limit 1";
		$row = $this->adb->getRow($query);
		
		return $row['categories_id'];
	}
	
	function getCategory($items_id) {
		$query = "select ic.* from pp_contents_items_categories ic, pp_contents_items i where ic.categories_id = i.categories_id " 
				. " and i.items_id = '" . (int)$items_id . "' limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getCategories($items_id) {
		$query = "select ic.* from pp_contents_items_categories ic, pp_contents_items i where ic.categories_id = i.categories_id " 
				. " and i.items_id = '" . (int)$items_id . "'";
		return $this->adb->getRows($query);
	}
	
	function getCategoriesID($items_id) {
		$query = "select categories_id from pp_contents_items where items_id = '" . (int)$items_id . "' limit 1";
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$rows[] = $row['categories_id'];
		}
		
		return $rows;
	}
	
	//private
	function _choice_value($choice, $value) {
		if( $choice > 0 || is_array($value) ) {
			$value = '';
		}
		
		return $value;
	}
	
	function _choice_values($choice, $value) {
		if( $choice > 0 && !is_array($value) ) {
			$ls = explode('\r\n', $value);
			$value = array();
			for($i=0, $n=sizeof($ls); $i<$n; $i++) {
				$value[] = $ls[$i];
			}
		}
		
		return $value;
	}
}
//
?>